/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.peer.ui.async.gui;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.ourgrid.common.interfaces.to.WorkerInfo;
import org.ourgrid.peer.ui.async.client.PeerAsyncInitializer;

/**
 * It represents a panel where it is possible to view the status of the workers.
 */
public class WorkerTablePanel extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	
	/** Creates new form WorkerTablePanel */
    public WorkerTablePanel() {
    	initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tableScrollPane = new javax.swing.JScrollPane();
        workerTable = new javax.swing.JTable();

        workerTable.setModel(getTableModel());
        workerTable.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		if (workerTable.getSelectedColumn() == WorkerTableModel.DELETE_COLUMN) {
        			deleteTableRow(workerTable.getSelectedRow());
        		}
        	}
        	
/*        	@Override
        	public void mouseEntered(MouseEvent e) {
        		if (workerTable.getSelectedColumn() == WorkerTableModel.STATUS_COLUMN) {
        			ImageIcon icon = (ImageIcon) workerTable.getValueAt(workerTable.getSelectedRow(), workerTable.getSelectedColumn());
        			workerTable.setToolTipText(icon.getDescription());
        		}
        	}
*/        });
        tableScrollPane.setViewportView(workerTable);
        
        BorderLayout bLayout = new BorderLayout();
        this.setLayout(bLayout);
        add(tableScrollPane, BorderLayout.CENTER);
        
        Box box = Box.createHorizontalBox();
        box.add(new JLabel("IDLE   ", new ImageIcon(WorkerTableModel.WORKER_IDLE_IMAGE_PATH, "IDLE"), JLabel.LEFT));
        box.add(new JLabel("CONTACTING   ", new ImageIcon(WorkerTableModel.WORKER_CONTACTING_IMAGE_PATH, "CONTACTING"), JLabel.LEFT));
        box.add(new JLabel("OWNER   ", new ImageIcon(WorkerTableModel.WORKER_OWNER_IMAGE_PATH, "OWNER"), JLabel.LEFT));
        box.add(new JLabel("DONATED   ", new ImageIcon(WorkerTableModel.WORKER_DONATED_IMAGE_PATH, "DONATED"), JLabel.LEFT));
        box.add(new JLabel("INUSE   ", new ImageIcon(WorkerTableModel.WORKER_INUSE_IMAGE_PATH, "INUSE"), JLabel.LEFT));
        
        add(box, BorderLayout.SOUTH);

        /*org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(tableScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 464, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(tableScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                .addContainerGap())
        );*/
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * Removes the specified row of this table.
     * @param selectedRow The row to be removed.
     */
    private void deleteTableRow(int selectedRow) {
    	
    	WorkerInfo workerInfo = getTableModel().getWorkerInfo(selectedRow);
    	
    	int confirmDeletion = JOptionPane.showConfirmDialog(null, "Delete worker [" + workerInfo.getId() + "] ?", 
    			"Delete worker", JOptionPane.YES_NO_OPTION);
    	
    	if (confirmDeletion == JOptionPane.YES_OPTION) {
    		
//    		getTableModel().removeRow(selectedRow);
    		PeerAsyncInitializer.getInstance().getComponentClient().removeWorker(workerInfo.getWorkerSpec());
    		
//    		getTableModel().fireTableRowsDeleted(selectedRow, selectedRow);
    		
    	}
    	
	}

	/**
     * Returns the table that displays the status of the worker.
     * @return The table that displays the status of the worker.
     */
    private WorkerTableModel getTableModel() {
    	if (model == null) {
    		model = new WorkerTableModel(new LinkedList<WorkerInfo>());
    	}
    	return model;
    }
    
    /**
     * Defines the informations about the workers.
     * @param data The informations about the workers.
     */
    public void setTableModelData(Collection<WorkerInfo> data) {
    	getTableModel().setData(data);
    	getTableModel().fireTableDataChanged();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane tableScrollPane;
    private javax.swing.JTable workerTable;
    // End of variables declaration//GEN-END:variables
    
    private WorkerTableModel model;

	public void peerStopped() {
		setTableModelData(Collections.<WorkerInfo>emptyList());		
	}

}
